<template>
	<view>
		<view>
			<view class='header'>
				<image src='../static/logo.png'></image>
			</view>
			<view class='content'>
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像、地区等)</text>
			</view>

			<button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN"
				@getuserinfo="wxGetUserInfo">
				授权登录
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickName: '',
				avatarUrl: '',
				code: '',
			};
		},
		methods: {
			wxGetUserInfo() {
				var that = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(res) {
						console.log(res)
						uni.setStorage({
							key: 'nickName',
							data: res.userInfo.nickName,
						});
						uni.setStorage({
							key: 'gender',
							data: res.userInfo.gender,
						});
						uni.setStorage({
							key: 'avatarUrl',
							data: res.userInfo.avatarUrl,
						});
						uni.setStorage({		
							key: 'city',
							data: res.userInfo.city,
						});
						that.nickName = res.userInfo.nickName;
						that.login();
					},
					fail(fail) {}
				});
			}, //登录
			login() {
				let that = this;
				// wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(res) {
						const code = res.code;
						uni.getUserProfile({
							provider: 'weixin',
							success: function(res) {
								that.wxGetUserInfo()
								// console.log(res, '12321321323')
								// that.nickName = res.userInfo.nickName;
								// that.avatarUrl = res.userInfo.avatarUrl;
								// that.province = res.userInfo.province;
							}
						});
						uni.request({
							url: 'http://localhost:8070/web/api/rest/User', //登录的域名完整链接，可以换成你们自己的。
							async: false,
							data: {
								code: code,
								nickName: that.nickName,
								avatarUrl: that.avatarUrl,
								province: that.province,
							},
							method: 'POST',
							header: {
								'content-type': 'application/json'
							},
							success: (res) => {
								console.log(res,'asdasdsad')
								uni.reLaunch({ //成功后跳转到小程序首页，由于没得界面，我就屏蔽了。
									url: '/pages/index/index'
								});
							}
						});
					},
				});
			},
		},
		onLoad() {

		}
	}
</script>

<style>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
